package com.sunny.user.action;

import com.opensymphony.xwork2.ActionContext;
import com.sunny.util.json.JsonUtils;
import com.sunny.util.struts2.BaseAction;
import com.sunny.util.struts2.Struts2Utils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * Created by tongchengke on 2016/8/25.
 */
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<Object>{

    /**
     * 跳转到登录界面
     * @return
     */
    public String toLoginView(){
        return "login";
    }

    /**
     * 登录验证
     * @return
     */
    public void loginValidate(){

        java.util.Map<String, Object> user = new java.util.HashMap<String, Object>();


        Map<String, Object> session = ActionContext.getContext().getSession();
        session.put("user",user);

        String json = "";
        if("admin".equalsIgnoreCase(name)){
            user.put("name","admin");
            user.put("email","aa@email.com");
            json = JsonUtils.toJson(user);
        } else {
            json = "aa" ;
        }
        Struts2Utils.renderText(json);
    }

    /**
     * 跳转到首页
     * @return
     */
    public String toAzyIndexView(){
        Map<String, Object> session = ActionContext.getContext().getSession();

        java.util.Map<String, Object> user = (Map<String, Object>) session.get("user");
        if (user!=null && "admin".equalsIgnoreCase(user.get("name")+"")) {
            return "azyIndex";
        } else {
            return "loginView";
        }
    }

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Object getModel() {
        return null;
    }
}
